package br.com.vacine.dao;

import java.lang.reflect.Field;

import br.com.vacine.dao.DBsqLite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;


public class DBGeneric {


private SQLiteDatabase db;
	
	public DBGeneric(Context contex){
		DBsqLite auxDb = new DBsqLite(contex);
		db = auxDb.getWritableDatabase();
	}
	
	public void insertOld(Object obj, String campos[], String dados[]){
		
		String nomeTabela = "usuario";
		
		StringBuilder str = new StringBuilder();
		
		str.append("INSERT INTO ").append(nomeTabela).append("(");
		
		
		for (int i = 0; i < campos.length; i++) {
			
			if(i == 1 ){
				str.append(campos[i].toString());
			} else {
				
				str.append(",").append(campos[i].toString());
			}
		}
		str.append(")");
		
		str.append("VALUES(");
		
		for (int i = 0; i < dados.length; i++) {
			
			if(i == 1){
				
				str.append(dados[i].toString());
			} else{
				
				str.append(",").append(dados[i].toString());
			}
		}
		
		str.append(");");
	}
	
public void insert(Object obj){
		Class ob = obj.getClass();
		Field campos[] = ob.getDeclaredFields();
		
		String nomeTabela = "usuario";
		
		StringBuilder str = new StringBuilder();
		
		str.append("INSERT INTO ").append(nomeTabela).append("(");
		
		for (int i = 0; i < campos.length; i++) {
			if(i == 0 ){
				str.append(campos[i].getName());
			} else {
				
				str.append(",").append(campos[i].getName());
			}			
		}		

		str.append(")");
		
		str.append("VALUES(");
		
		for (int i = 0; i < campos.length; i++) {
			
			Field field;
			try {
				field = ob.getDeclaredField("idusuario");
				Object vl = field.get(ob);
				Object valor =vl;	
			} catch (NoSuchFieldException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
			if(i == 0){
				
				try {
				//	str.append(valores[i].get(obj));
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else{
				
				try {
				//	str.append(",").append(vl);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		str.append(");");
	}
	
	public void update(Object obj, String campos[], String dados[], String id){
		
		String nomeTabela = "";
		
		StringBuilder str = new StringBuilder();
		
		str.append("UPDATE ").append(nomeTabela).append("SET");
		
		
		for (int i = 0; i < campos.length; i++) {
			
			if(i == 1 ){
				str.append(campos[i].toString()).append("=").append(dados[i].toString());
			} else {
				
				str.append(",").append(campos[i].toString()).append(dados[i].toString());
			}
		}
		
		str.append("WHERE id"+nomeTabela).append("=").append(id);

		

		
	}
	
	public void delete(Object obj, String id){
		
		String nomeTabela = "";
		
		StringBuilder str = new StringBuilder();
		
		str.append("DELETE FROM ").append(nomeTabela);
		
		str.append("WHERE id"+nomeTabela).append("=").append(id);

	}
	
	public void list(Object obj, String id){
		
	
	}

	public SQLiteDatabase getDb() {
		return db;
	}

	public void setDb(SQLiteDatabase db) {
		this.db = db;
	}
	
	
}
